<?php
class Account_permission_model extends MY_Model
{
	public
		$table = 'account_permissions',
		$prKey = 'id'		
	;

	function __construct()
	{
		parent::__construct();

		$this->fields = array(
			'id_account_type' 		=> T_('Role'),
			'id_account_resources' 	=> T_('Resource'),
		);

		$this->rules = array(
			'id_account_type'		=> "trim|strip_tags|integer",
			'id_account_resources'	=> "trim|strip_tags|integer",
		);
	}
	
	function search($config = array())
	{		
		$sql = 'SELECT re.id AS id_resource, re.category, re.description, pe.id_account_type';
		$sql .= ' FROM acl_resources re';
		$sql .= ' LEFT JOIN account_permissions pe ON re.id=pe.id_account_resources';
		$sql .= ' LEFT JOIN acl_roles ro ON ro.id=pe.id_account_type';
		$sql .= ' ORDER BY re.controller, re.id';
		
		$this->data['list'] = $this->db->query($sql)->result(); 
	}

	function get($id_role = null)
	{	
	  $WHERE = (!empty($id_role)) ? ' WHERE id_account_type='.$id_role : '';	
		
		$sql = 'SELECT * FROM ' . $this->table . $WHERE;  
		$query = $this->db->query($sql);
		
		$data = array();
		
		foreach ($query->result() as $row)
		{
			$data[$row->id_account_resources][$row->id_account_type] = $row->id_account_type; 
		}
				
		return $data; 
	}		
}
?>